<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/8
 * Time: 12:49
 */

namespace app\admin\controller;


use app\admin\logic\MenuLogic;
use app\admin\logic\RoleLogic;
use app\common\lib\Arr;
use app\common\lib\Tree;
use think\App;
use think\Db;

class Role extends Base
{
    protected $logic = null;
    protected $menu = null;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->logic = new RoleLogic();
        $this->menu  = new MenuLogic();
    }

    public function index()
    {
        $where           = [];
        $where['status'] = 1;
        $lists           = $this->logic->getLists($where);

        return view('role/index', compact('lists'));
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            try {
                $res = $this->logic->saveData($data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($res) {
                $this->success($data['title']."角色添加成功", url("Role/index"));
            }
            $this->error("添加失败");

        } else {
            $where            = [];
            $where['field']   = [
                'id',
                'name',
                'title',
                'pid',
            ];
            $where['status']  = 1;
            $where['order']   = "id asc";
            $lists            = $this->menu->getLists($where);
            $tree             = Tree::instance();
            $permissions      = $tree->init($lists, 'pid');
            $permissions      = $tree->getTreeList($tree->getTreeArray('0'), 'title');
            $this->assign('all_permissions', $permissions);

            return view();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $id   = $this->request->post('id', 0, 'intval');
            $data = [
                'title'       => $this->request->post('title', '', 'trim'),
                'status'      => $this->request->post('status', 0, 'intval'),
                'rules'       => implode(',', $this->request->post('rules')),
                'update_time' => time(),
            ];
            try {
                $res = $this->logic->updateById($id, $data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            if ($res) {
                $this->success($data['title'].'编辑成功', url('Role/index'));
            }
            $this->error('编辑失败');
        } else {
            $id       = $this->request->param('id', 0, 'intval');
            $role_one = $this->logic->findOne($id);
            if (empty($role_one)) {
                $this->error('该角色不存在');
            }

            $where            = [];
            $where['field']   = [
                'id',
                'name',
                'title',
                'pid',
            ];
            $where['status']  = 1;
            $where['order']   = "id asc";
            $lists            = $this->menu->getLists($where);
            $tree             = Tree::instance();
            $permissions      = $tree->init($lists, 'pid');
            $permissions      = $tree->getTreeList($tree->getTreeArray('0'), 'title');


            return view('role/edit', compact('role_one', 'permissions'));
        }
    }

    /**
     * 删除菜单
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        try {
            $res = $this->logic->deleteById($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if ($res) {
            $this->success("删除成功", url('Role/index'));
        }
        $this->error('删除失败');
    }
}